<?php
// @formatter:off
/**
 * @file s.tags.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: s.tags.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/services/s.tags.php $
 */
// @formatter:on

$wcServiceStatus = false;
$wcServiceErrorCode = 500;

if(defined('__SERVICE__') && $wcGroup && !$wcGroupForbidden && $wcProfile->allowedAny(WC_PERM_COMIC_VIEW, WC_PERM_TAG_LIST, WC_PERM_DIRECTORY_VIEW, WC_PERM_TAG_VIEW, WC_PERM_COMIC_SET_TAG, WC_PERM_DIRECTORY_SET_TAG, WC_PERM_TAG_SET_TAG, WC_PERM_TAG_RENAME)) {
	/*
	 * Conditions for GET method (not POST):
	 *	- When there's a comic set ($wcComic) and the requesting user is
	 *	  allowed to view comics and list tags, the service returns the
	 *	  list of tags assigned to the comic.
	 *	- When there's a directory set ($wcDirectory) and the requesting
	 *	  user is allowed to view directories and list tags, the service
	 *	  returns the list of tags assigned to the comics in the specified
	 *	  directory.
	 *	- When there's a tag set ($wcTag) and the requesting user is
	 *	  allowed to view and list tags, the service returns the list of
	 *	  tags assigned to the comic that have $wcTag assigned (... no,
	 *	  it's not inception).
	 *	- When there's no comic, tag or directory set, but there's a
	 *	  parameter called "pattern" set and the requesting user is
	 *	  allowed to view and list tags, the service returns the list of
	 *	  tags that match with the specified pattern.
	 *	- Otherwise, returns a error message.
	 *
	 * Conditions for POST method:
	 *	- When there's a POST parameter called MODE set, another called
	 *	  LIST and the requesting user is allow to list tags, the service
	 *	  may (LIST is a list of tag names of not necessarily existing
	 *	  tags):
	 *		- If there's a comic specified ($wcComic), the requesting
	 *		  user is allowed to set comic tags and the parameter MODE
	 *		  is:
	 *			- "ADD", the list of tags is assigned to this
	 *			  comic.
	 *			- "REMOVE", the list of tags is unassigned from
	 *			  this comic if it has any of them.
	 *		- If there's a directory specified ($wcDirectory), the
	 *		  requesting user is allowed to set directory tags and the
	 *		  parameter MODE is:
	 *			- "ADD", the list of tags is assigned to each
	 *			  comic of this directory.
	 *			- "REMOVE", the list of tags is unassigned from
	 *			  any comic of this directory.
	 *		- If there's a tag specified ($wcTag), the requesting user
	 *		  is allowed to set tag tags and the parameter MODE is:
	 *			- "ADD", the list of tags is assigned to each
	 *			  comic of this tag.
	 *			- "REMOVE", the list of tags is unassigned from
	 *			  any comic of this tag.
	 *	- If there's a tag specified ($wcTag), the parameter "rename" is
	 *	  set ($_REQUEST), the POST parameter NEXT_NAME is set and the
	 *	  requesting user is allowed to rename tags, the service rename
	 *	  the specified tag with NEXT_NAME.
	 *	- Otherwise, returns a error message.
	 */
	if($_SERVER["REQUEST_METHOD"] != "POST") {
		//
		// Loading global pointers.
		global $wcSpecialTags;

		if($wcComic && $wcProfile->allowedTo(WC_PERM_COMIC_VIEW, WC_PERM_TAG_LIST)) {
			//
			// Comic tags list requested.
			$wcServiceStatus = true;
			foreach($wcTagsHolder->tagsOf($wcComic) as $tag) {
				$wcServiceXMLResult .= "<tag><id>{$tag->id}</id><name>{$tag->name}</name>";
				if(in_array($tag->name, $wcSpecialTags)) {
					$wcServiceXMLResult .= "<special/>";
				}
				$wcServiceXMLResult .= "</tag>";
			}
		} elseif($wcDirectory && $wcProfile->allowedTo(WC_PERM_DIRECTORY_VIEW, WC_PERM_TAG_LIST)) {
			//
			// Directory tags list requested.
			$wcServiceStatus = true;
			foreach($wcTagsHolder->tagsOfDirectory($wcDirectory) as $tag) {
				$wcServiceXMLResult .= "<tag><id>{$tag->id}</id><name>{$tag->name}</name>";
				if(in_array($tag->name, $wcSpecialTags)) {
					$wcServiceXMLResult .= "<special/>";
				}
				$wcServiceXMLResult .= "</tag>";
			}
		} elseif($wcTag && $wcProfile->allowedTo(WC_PERM_TAG_VIEW, WC_PERM_TAG_LIST)) {
			//
			// Tag tags list requested.
			$wcServiceStatus = true;
			foreach($wcTagsHolder->tagsOfTag($wcTag) as $tag) {
				$wcServiceXMLResult .= "<tag><id>{$tag->id}</id><name>{$tag->name}</name>";
				if(in_array($tag->name, $wcSpecialTags)) {
					$wcServiceXMLResult .= "<special/>";
				}
				$wcServiceXMLResult .= "</tag>";
			}
		} elseif(isset($_REQUEST["pattern"]) && $wcProfile->allowedTo(WC_PERM_TAG_LIST)) {
			$wcPageCache->anyTag();

			$pattern = $_REQUEST["pattern"];
			$limit = isset($_REQUEST["limit"]) ? $_REQUEST["limit"] : 20;

			$wcServiceStatus = true;
			foreach($wcTagsHolder->tagsLike($pattern, $wcGroup) as $tag) {
				$wcServiceXMLResult .= "<tag><id>{$tag->id}</id><name>{$tag->name}</name>";
				if(in_array($tag->name, $wcSpecialTags)) {
					$wcServiceXMLResult .= "<special/>";
				}
				$wcServiceXMLResult .= "</tag>";

				$limit--;
				if(!$limit) {
					break;
				}
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	} elseif($_SERVER["REQUEST_METHOD"] == "POST") {
		if(isset($_POST["MODE"]) && isset($_POST["LIST"]) && $wcProfile->allowedTo(WC_PERM_TAG_LIST)) {
			$tags = array();
			//
			// Transforming the list of tag names into a list of WCTag
			// objects.
			// If it doesn't exists, it is created.
			foreach($_POST["LIST"] as $name) {
				$name = trim($name);
				if($name) {
					$tag = $wcTagsHolder->itemByName($name, $wcGroup);
					if($tag) {
						$tags[] = $tag;
					} elseif($wcTagsHolder->create($name, $wcGroup)) {
						$tags[] = $wcTagsHolder->itemByName($name, $wcGroup);
					}
				}
			}

			$wcErrno = 0;
			$wcError = "";
			if($wcComic && $wcProfile->allowedTo(WC_PERM_COMIC_SET_TAG)) {
				if($_POST["MODE"] == "ADD") {
					$wcServiceStatus = true;
					foreach($tags as $tag) {
						$tag->add($wcComic);
						if($wcErrno != 0 && count($tags) == 1) {
							$wcServiceStatus = false;
							$wcServiceErrorCode = $wcErrno;
							$wcServiceErrorMsg = $wcError;
						}
					}
				} elseif($_POST["MODE"] == "REMOVE") {
					$wcServiceStatus = true;
					foreach($tags as $tag) {
						$tag->removeComic($wcComic);
						if($wcErrno != 0 && count($tags) == 1) {
							$wcServiceStatus = false;
							$wcServiceErrorCode = $wcErrno;
							$wcServiceErrorMsg = $wcError;
						}
					}
				} else {
					$wcServiceErrorMsg = "Unknown mode";
				}
			} elseif($wcDirectory && $wcProfile->allowedTo(WC_PERM_DIRECTORY_SET_TAG)) {
				if($_POST["MODE"] == "ADD") {
					$wcServiceStatus = true;
					foreach($tags as $tag) {
						$tag->addDirectory($wcDirectory);
					}
				} elseif($_POST["MODE"] == "REMOVE") {
					$wcServiceStatus = true;
					foreach($tags as $tag) {
						$tag->removeDirectory($wcDirectory);
					}
				} else {
					$wcServiceErrorMsg = "Unknown mode";
				}
			} elseif($wcTag && $wcProfile->allowedTo(WC_PERM_TAG_SET_TAG)) {
				if($_POST["MODE"] == "ADD") {
					$wcServiceStatus = true;
					foreach($tags as $tag) {
						$tag->addTag($wcTag);
					}
				} elseif($_POST["MODE"] == "REMOVE") {
					$wcServiceStatus = true;
					foreach($tags as $tag) {
						$tag->removeTag($wcTag);
					}
				} else {
					$wcServiceErrorMsg = "Unknown mode";
				}
			} else {
				$wcServiceErrorMsg = "This service was call with invalid parameters";
			}
		} elseif($wcTag && isset($_REQUEST["rename"]) && isset($_POST["NEXT_NAME"]) && strlen(trim($_POST["NEXT_NAME"])) && $wcProfile->allowedTo(WC_PERM_TAG_RENAME)) {
			$next = trim($_POST["NEXT_NAME"]);
			$name = $wcTag->name;

			if($wcTag->isSpecial()) {
				$wcServiceErrorMsg = "Tag '{$wcTag->name}' is a special tag";
			} elseif(!$next) {
				$wcServiceErrorMsg = "No new name specified";
			} elseif($next == $name) {
				$wcServiceErrorMsg = "New name is the same than current one";
			} else {
				$tag = @$wcTagsHolder->itemByName($next, $wcGroup);
				if(!$tag) {
					$wcServiceStatus = true;
					$wcTag->name = $next;
				} else {
					$wcServiceErrorMsg = "Tag '{$tag->name}' already exists";
				}
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	} else {
		$wcServiceErrorMsg = "This service doesn't work on {$_SERVER["REQUEST_METHOD"]} method";
	}
} else {
	$wcServiceErrorCode = 403;
	$wcServiceErrorMsg = "Unable to access service";
}
?>